package com.jxstjh.conch.handler.pending;

import com.dtp.core.thread.DtpExecutor;
import com.jxstjh.conch.handler.config.HandlerThreadPoolConfig;
import com.jxstjh.conch.handler.utils.GroupIdMappingUtil;
import com.jxstjh.conch.support.utils.ThreadPoolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * 缓冲区配置
 * 消息类型 与 TaskPending 映射关系
 */
@Component
public class TaskPendingHolder {

    @Autowired
    private ThreadPoolUtil threadPoolUtil;

    private Map<String, ExecutorService> taskPendingHolder = new HashMap<>(32);

    private static List<String> groupIds = GroupIdMappingUtil.getAllGroupIds();

    /**
     * 给每个渠道，每种消息类型初始化一个线程池
     */
    @PostConstruct
    public void init() {
        /**
         * 线程池名称示例   conch.sms.notice
         */
        for (String groupId : groupIds) {
            DtpExecutor executor = HandlerThreadPoolConfig.getExecutor(groupId);
            threadPoolUtil.register(executor);
            taskPendingHolder.put(groupId, executor);
        }
    }

    /**
     * 得到对应的线程池
     *
     * @param groupId
     * @return
     */
    public ExecutorService route(String groupId) {
        return taskPendingHolder.get(groupId);
    }
}